home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MACSHELL / MS1 / COMMANDS / RESOURCE.C < prev    next >
Text File  |  1992-12-02  |  24KB  |  1,046 lines

  1. /*
  2.  *    MacShell Source File
  3.  *
  4.  *    Copyright (c) 1989, 1990, 1991, 1992  Suick Bay Technologies.  All rights reserved.
  5.  *
  6.  *
  7.  *    RESTRICTIONS ON MacShell program and source code.
  8.  *
  9.  *    Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
  10.  *    restricted use by the owner of the CDROM "Disk to the future II".
  11.  *
  12.  *    Ñ╩No permission is granted for any commercial use without the written
  13.  *    consent of the Suick Bay Technologies.
  14.  *
  15.  *    Ñ╩No permission is granted for any redistribution of any kind use without
  16.  *    the written consent of the Suick Bay Technologies.
  17.  *
  18.  *    Ñ╩Permission is granted to use this for any personal noncommercial use.
  19.  *
  20.  *    Ñ╩You may not distribute source or executable code at all, nor may you 
  21.  *    distribute it with or within a commercial product without the written
  22.  *    consent of the Suick Bay Technologies.  Please send modifications to 
  23.  *    the author for inclusion in updates to the program.  Thanks.
  24.  *
  25.  *
  26.  *    MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  27.  *    WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  28.  *    PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  29.  *
  30.  *    SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  31.  *    INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
  32.  *    OR ANY PART THEREOF. 
  33.  *
  34.  *    In no event will Suick Bay Technologies be liable for any lost revenue
  35.  *    or profits or other special, indirect and consequential damages, even if
  36.  *    Suick Bay Technologies has been advised of the possibility of such damages.
  37.  *
  38.  *    Suick Bay Technologies can be reached at:
  39.  *    
  40.  *    8768 Cottonwood lane
  41.  *    Maple Grove, MN 55369
  42.  *    Voice: (612) 425-7025
  43.  *    AppleLink: D5233
  44.  *    
  45.  *
  46.  *    No parts of this software may be reproduced or stored in a
  47.  *    retrieval system or transmitted in any form, or any means,
  48.  *    electronic, mechanical, photocopying, recording or otherwise,
  49.  *    without the prior written permission of Suick Bay Technologies.
  50.  *    
  51.  *    Spread the word and not the disk.
  52.  *    
  53.  *    SPK 012290    :    Initial
  54.  *
  55.  *    cpr        - copy resource 
  56.  *    mvr        - move resource 
  57.  *    cmpr    - compare resource 
  58.  *    rmr        - remove resource
  59.  *
  60.  *    grepr    - resource search
  61.  *
  62.  */
  63.  
  64. #include    <ResourceMgr.h>
  65.  
  66. #include    "SystemPub.h"
  67. #include    "Proc.h"
  68. #include    "ShellPub.h"
  69. #include    "Path.h"
  70.  
  71. /*******************************************************************/
  72.  
  73. #define        f1Type        (**MyShell).Proc[ProcID].long0
  74. #define        f2Type        (**MyShell).Proc[ProcID].long1
  75. #define        f1ID        (**MyShell).Proc[ProcID].int0
  76. #define        f2ID        (**MyShell).Proc[ProcID].int1
  77.  
  78. int16        OpenResCmdFile( WHandle ShellWh, char *path, int16 *wasOpen )
  79. {
  80. int16        srcVref, failed = FALSE, srcFRefNum;
  81. int32        srcDirID, temp;
  82. pathType    pt;
  83. char        sName[ 64 ];
  84. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  85.  
  86.     pt = SetCurrPath( path );
  87.     if( pt == pathIsFile )
  88.         {
  89.         strcpy( sName, GetLastScan() );
  90.         
  91.         GetPWDInfo( &srcVref, &srcDirID, &temp );
  92.         CtoPstr( sName );
  93.         
  94.         *wasOpen = FALSE;
  95.         if( ResFileOpen( sName, srcVref, srcDirID, &srcFRefNum ) )
  96.             {
  97.             *wasOpen = TRUE;
  98.             }
  99.         else
  100.             srcFRefNum = OpenResFile( sName );
  101.         }
  102.     else
  103.         failed = TRUE;
  104.         
  105.     if( failed )
  106.         return( -1 );
  107.     else
  108.         return( srcFRefNum );
  109.     
  110.     ResetShellPWD( ShellWh );
  111. }
  112.  
  113. /*******************************************************************
  114.  *
  115.  *        cpr file1 type1 id1 file2 [type2 [id2]]
  116.  *
  117.  *******************************************************************/
  118.  
  119. void        CPRes( WHandle ShellWh, int16 ProcID, char *file1, char *file2 )
  120. {
  121. int16        err, currFile, srcWasOpen, dstWasOpen, 
  122.             srcFRefNum, dstFRefNum, failed = FALSE;
  123. Handle        h;
  124. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  125.  
  126.     currFile = CurResFile();
  127.     SetResLoad( FALSE );
  128.  
  129.     srcFRefNum = OpenResCmdFile( ShellWh, file1, &srcWasOpen );
  130.     
  131.     if( srcFRefNum != (-1) )
  132.         {
  133.         if( strcmp( file1, file2 ) == 0 )
  134.             {
  135.             dstFRefNum = srcFRefNum;
  136.             dstWasOpen = TRUE;
  137.             }
  138.         else
  139.             dstFRefNum = OpenResCmdFile( ShellWh, file2, &dstWasOpen );
  140.         
  141.         if( dstFRefNum != (-1) )
  142.             {
  143.             UseResFile( srcFRefNum );            /* get the resource */
  144.             
  145.             h = Get1Resource( f1Type, f1ID );
  146.             err = ResError();
  147.             
  148.             if( h && !err )
  149.                 {
  150.                 LoadResource( h );
  151.                 DetachResource( h );
  152.                 
  153.                 UseResFile( dstFRefNum );
  154.                 AddResource( h, f2Type, f2ID, "\p" );    /* we are blowing off the name */
  155.                 WriteResource( h );
  156.                 }
  157.  
  158.             if( !dstWasOpen )
  159.                 CloseResFile( dstFRefNum );
  160.             }
  161.         else
  162.             failed = TRUE;
  163.         
  164.         if( !srcWasOpen )
  165.             CloseResFile( srcFRefNum );
  166.         }
  167.     else
  168.         failed = TRUE;
  169.     
  170.     if( failed )
  171.         procPrintf( ShellWh, ProcID, "cpr : can't copy resource.\n" );
  172.     
  173.     SetResLoad( TRUE );
  174.     UseResFile( currFile );
  175. }
  176.  
  177. /*******************************************************************
  178.  *
  179.  *        mvr file1 type1 id1 file2 [type2 [id2]]
  180.  *
  181.  *******************************************************************/
  182.  
  183. void        MVRes( WHandle ShellWh, int ProcID, char *file1, char *file2 )
  184. {
  185. int16        err, currFile, srcWasOpen, dstWasOpen, 
  186.             srcFRefNum, dstFRefNum, failed = FALSE;
  187. Handle        h;
  188. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  189.  
  190.     currFile = CurResFile();
  191.     SetResLoad( FALSE );
  192.  
  193.     srcFRefNum = OpenResCmdFile( ShellWh, file1, &srcWasOpen );
  194.     
  195.     if( srcFRefNum != (-1) )
  196.         {
  197.         if( strcmp( file1, file2 ) == 0 )
  198.             {
  199.             dstFRefNum = srcFRefNum;
  200.             dstWasOpen = TRUE;
  201.             }
  202.         else
  203.             dstFRefNum = OpenResCmdFile( ShellWh, file2, &dstWasOpen );
  204.         
  205.         if( dstFRefNum != (-1) )
  206.             {
  207.             UseResFile( srcFRefNum );            /* get the resource */
  208.             
  209.             h = Get1Resource( f1Type, f1ID );
  210.             err = ResError();
  211.             
  212.             if( h && !err )
  213.                 {
  214.                 LoadResource( h );
  215.                 DetachResource( h );
  216.                 
  217.                 UseResFile( dstFRefNum );
  218.                 AddResource( h, f2Type, f2ID, "\p" );    /* we are blowing off the name */
  219.                 err = ResError();
  220.                 
  221.                 WriteResource( h );
  222.                 err = ResError();
  223.                 
  224.                 if( !err )    /* delete the original resource */
  225.                     {
  226.                     UseResFile( srcFRefNum );    /* get the resource */
  227.                     h = Get1Resource( f1Type, f1ID );
  228.                     if( h )
  229.                         {
  230.                         LoadResource( h );    
  231.                         RmveResource( h );
  232.                         DisposHandle( h );
  233.                         }
  234.                     }
  235.                 }
  236.  
  237.             if( !dstWasOpen )
  238.                 CloseResFile( dstFRefNum );
  239.             }
  240.         else
  241.             failed = TRUE;
  242.         
  243.         if( !srcWasOpen )
  244.             CloseResFile( srcFRefNum );
  245.         }
  246.     else
  247.         failed = TRUE;
  248.     
  249.     if( failed )
  250.         procPrintf( ShellWh, ProcID, "mvr : can't move resource.\n" );
  251.  
  252.     SetResLoad( TRUE );
  253.     UseResFile( currFile );
  254. }
  255.  
  256. /*******************************************************************
  257.  *
  258.  *        cmpr file1 type1 id1 file2 [type2 [id2]]
  259.  *
  260.  *******************************************************************/
  261.  
  262. void        CMPRes( WHandle ShellWh, int ProcID, char *file1, char *file2 )
  263. {
  264. int16        err, currFile, f1WasOpen, f2WasOpen, 
  265.             f1FRefNum, f2FRefNum, failed = FALSE;
  266. Handle        h1, h2;
  267. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  268.  
  269.     currFile = CurResFile();
  270.     SetResLoad( FALSE );
  271.  
  272.     f1FRefNum = OpenResCmdFile( ShellWh, file1, &f1WasOpen );
  273.     
  274.     if( f1FRefNum != (-1) )
  275.         {
  276.         if( strcmp( file1, file2 ) == 0 )
  277.             {
  278.             f2FRefNum = f1FRefNum;
  279.             f2WasOpen = TRUE;
  280.             }
  281.         else
  282.             f2FRefNum = OpenResCmdFile( ShellWh, file2, &f2WasOpen );
  283.         
  284.         if( f2FRefNum != (-1) )
  285.             {
  286.             UseResFile( f1FRefNum );            /* get the resource */
  287.             
  288.             h1 = Get1Resource( f1Type, f1ID );
  289.             err = ResError();
  290.             
  291.             UseResFile( f2FRefNum );            /* get the resource */
  292.             h2 = Get1Resource( f2Type, f2ID );
  293.             err = ResError();
  294.  
  295.             if( h1 && h2 )    /* compare the resources */
  296.                 {
  297.                 int32    len1, len2;
  298.                 
  299.                 LoadResource( h1 );
  300.                 LoadResource( h2 );
  301.                 
  302.                 /* compare lengths */
  303.                 
  304.                 len1 = SizeResource( h1 );
  305.                 len2 = SizeResource( h2 );
  306.                 
  307.                 if( len1 != len2 )
  308.                     {
  309.                     procPrintf( ShellWh, ProcID, 
  310.                         "cmpr : resources different sizes.\n" );
  311.                     }
  312.                 else
  313.                     {
  314.                     char    *p1, *p2;
  315.                     
  316.                     /* compare contents */
  317.                     
  318.                     p1 = *h1;    /* no need to lock since we are not */
  319.                     p2 = *h2;    /* going to call any toolbox functions */
  320.                     
  321.                     while( len1 )
  322.                         {
  323.                         if( *p1 != *p2 )
  324.                             {
  325.                             procPrintf( ShellWh, ProcID, 
  326.                                 "cmpr : %4X %4X at index %ld (%lX Hex)\n",
  327.                                 *p1, *p2, len2 -len1, len2 -len1 );
  328.  
  329.                             break;
  330.                             }
  331.                             
  332.                         p1++;
  333.                         p2++;
  334.                         len1--;
  335.                         }
  336.                     
  337.                     if( len1 == 0 )
  338.                             procPrintf( ShellWh, ProcID, 
  339.                                 "cmpr : resources match.\n" );
  340.                     }
  341.                 }
  342.  
  343.             if( !f2WasOpen )
  344.                 CloseResFile( f2FRefNum );
  345.             }
  346.         else
  347.             failed = TRUE;
  348.         
  349.         if( !f1WasOpen )
  350.             CloseResFile( f1FRefNum );
  351.         }
  352.     else
  353.         failed = TRUE;
  354.     
  355.     if( failed )
  356.         procPrintf( ShellWh, ProcID, "cmpr : can't compare resources.\n" );
  357.     
  358.     SetResLoad( TRUE );
  359.     UseResFile( currFile );
  360. }
  361.  
  362. /*******************************************************************
  363.  *
  364.  *    Note DoRESCMD is the entry point for
  365.  *
  366.  *    cpr, mvr, cmpr
  367.  *
  368.  *******************************************************************/
  369.  
  370. Boolean            DoRESCMD( int16 ProcToken, WHandle ShellWh, int16 ProcID,
  371.                     char *string )
  372. {
  373. int16            i, argc, argsOk = TRUE;
  374. char            *cp, argument[ 64 ], file1[ 64 ], file2[ 64 ];
  375. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  376.  
  377.     switch( ProcToken )
  378.         {
  379.         case    PROC_INIT    :
  380.             (**MyShell).Proc[ ProcID ].flags = TRUE;
  381.             break;
  382.             
  383.         case    PROC_TERM    :
  384.         case    PROC_BREAK    :
  385.             /* Tell the shell that we're done */
  386.             SendOutToken( ShellWh, ProcID, PROC_BREAK );
  387.             /* Turn ourself off */
  388.             (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  389.             break;
  390.             
  391.         case    PROC_STDIN    :
  392.             if( (**MyShell).Proc[ ProcID ].flags )
  393.                 {
  394.                 (**MyShell).Proc[ ProcID ].flags = FALSE;        
  395.  
  396.                 /* get arguments */
  397.                 argc = (**MyShell).Proc[ ProcID ].argc;
  398.                 *file1 = '\0';
  399.                 *file2 = '\0';
  400.                 
  401.                 f1Type    = 0L;
  402.                 f2Type    = 0L;
  403.                 f1ID    = 0;
  404.                 f2ID    = 0;
  405.                 
  406.                 if( argc < 4 )
  407.                     {
  408.                     argsOk = FALSE;
  409.                     }
  410.                 else
  411.                     {
  412.                     for( i = 1; i < argc && (argsOk == TRUE); i++ )
  413.                         {
  414.                         GetArgv( ShellWh, ProcID, i, argument );
  415.                         
  416.                         switch( i )
  417.                             {
  418.                             case    1    :
  419.                                 strcpy( file1, argument );
  420.                                 break;
  421.                                 
  422.                             case    2    :    /* file 1 type */
  423.                                 f1Type = StrToType( argument );
  424.                                 f2Type = f1Type;
  425.                                 break;
  426.                                 
  427.                             case    3    :    /* file 1 ID */
  428.                                 CtoPstr( argument );
  429.                                 if( GoodNum( argument ) )
  430.                                     {
  431.                                     int32    tempVal;
  432.                                     
  433.                                     StringToNum( argument, &tempVal );
  434.                                     f1ID = (int16) tempVal;
  435.                                     f2ID = f1ID;
  436.                                     }
  437.                                 else
  438.                                     argsOk = FALSE;
  439.                                 break;
  440.                                 
  441.                             case    4    :
  442.                                 if( strcmp( argument, "." ) == 0 )
  443.                                     strcpy( file2, file1 );
  444.                                 else
  445.                                     strcpy( file2, argument );
  446.                                 break;
  447.                                 
  448.                             case    5    :    /* file 2 type    */
  449.                                 f2Type = StrToType( argument );
  450.                                 break;
  451.  
  452.                             case    6    :    /* file 2 ID    */
  453.                                 CtoPstr( argument );
  454.                                 if( GoodNum( argument ) )
  455.                                     {
  456.                                     int32    tempVal;
  457.                                     
  458.                                     StringToNum( argument, &tempVal );
  459.                                     f2ID = (int16) tempVal;
  460.                                     }
  461.                                 else
  462.                                     argsOk = FALSE;
  463.                                 break;
  464.                             }
  465.                         }
  466.                     }
  467.                 
  468.                 GetArgv( ShellWh, ProcID, 0, argument );
  469.                 if( argsOk )
  470.                     {
  471.                     if( strcmp( argument, "cpr" ) == 0 )
  472.                         CPRes( ShellWh, ProcID, file1, file2 );
  473.                     else if( strcmp( argument, "mvr" ) == 0 )
  474.                         MVRes( ShellWh, ProcID, file1, file2 );
  475.                     else if( strcmp( argument, "cmpr" ) == 0 )
  476.                         CMPRes( ShellWh, ProcID, file1, file2 );
  477.                     }
  478.                 else
  479.                     procPrintf( ShellWh, ProcID,
  480.                         "%s : bad arguments.\n", argument );
  481.  
  482.                 /* Tell the shell that we're done */
  483.                 SendOutToken(  ShellWh, ProcID, PROC_BREAK );
  484.                 
  485.                 /* Turn ourself off */
  486.                 (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  487.                 return( FALSE );
  488.                 }
  489.         }
  490. }
  491.  
  492. /*******************************************************************
  493.  *
  494.  *        rmr    file type [id]
  495.  *
  496.  *******************************************************************/
  497.  
  498. Boolean            DoRMR( int16 ProcToken, WHandle ShellWh, int16 ProcID,
  499.                     char *string )
  500. {
  501. int16            i, argc, argsOk = TRUE;
  502. char            *cp, argument[ 64 ], file1[ 64 ];
  503. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  504.  
  505.     switch( ProcToken )
  506.         {
  507.         case    PROC_INIT    :
  508.             (**MyShell).Proc[ ProcID ].flags = TRUE;
  509.             break;
  510.             
  511.         case    PROC_TERM    :
  512.         case    PROC_BREAK    :
  513.             /* Tell the shell that we're done */
  514.             SendOutToken( ShellWh, ProcID, PROC_BREAK );
  515.             /* Turn ourself off */
  516.             (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  517.             break;
  518.             
  519.         case    PROC_STDIN    :
  520.             if( (**MyShell).Proc[ ProcID ].flags )
  521.                 {
  522.                 (**MyShell).Proc[ ProcID ].flags = FALSE;        
  523.  
  524.                 /* get arguments */
  525.                 argc = (**MyShell).Proc[ ProcID ].argc;
  526.                 *file1 = '\0';
  527.                 
  528.                 f1Type    = 0L;
  529.                 f1ID    = 0;
  530.                 
  531.                 if( argc < 2 )
  532.                     {
  533.                     argsOk = FALSE;
  534.                     }
  535.                 else
  536.                     {
  537.                     for( i = 1; i < argc && (argsOk == TRUE); i++ )
  538.                         {
  539.                         GetArgv( ShellWh, ProcID, i, argument );
  540.                         
  541.                         switch( i )
  542.                             {
  543.                             case    1    :
  544.                                 strcpy( file1, argument );
  545.                                 break;
  546.                                 
  547.                             case    2    :    /* file 1 type */
  548.                                 f1Type = StrToType( argument );
  549.                                 f2Type = f1Type;
  550.                                 break;
  551.                                 
  552.                             case    3    :    /* file 1 ID */
  553.                                 CtoPstr( argument );
  554.                                 if( GoodNum( argument ) )
  555.                                     {
  556.                                     int32    tempVal;
  557.                                     
  558.                                     StringToNum( argument, &tempVal );
  559.                                     f1ID = (int16) tempVal;
  560.                                     f2ID = f1ID;
  561.                                     }
  562.                                 else
  563.                                     argsOk = FALSE;
  564.                                 break;
  565.                             }
  566.                         }
  567.                     }
  568.                 
  569.                 if( argsOk )
  570.                     {
  571.                     int16    currFile, fRefNum, wasOpen, cnt;
  572.                     Handle    h;
  573.                     
  574.                     currFile = CurResFile();
  575.                     SetResLoad( FALSE );
  576.  
  577.                     fRefNum = OpenResCmdFile( ShellWh, file1, &wasOpen );
  578.  
  579.                     if( fRefNum != (-1) )
  580.                         {
  581.                         if( argc == 3 )    /* remove all of type */
  582.                             {
  583.                             cnt = Count1Resources( f1Type ) + 1;
  584.                             while( --cnt )
  585.                                 {
  586.                                 h = Get1IndResource( f1Type, cnt );
  587.                                 if( h )
  588.                                     RmveResource( h );
  589.                                 }
  590.                             }
  591.                         else    /* remove type, id */
  592.                             {
  593.                             h = Get1Resource( f1Type, f1ID );
  594.                             if( h )
  595.                                 RmveResource( h );
  596.                             }
  597.                             
  598.                         if( !wasOpen )
  599.                             CloseResFile( fRefNum );
  600.                             
  601.                         SetResLoad( TRUE );
  602.                         }
  603.                     }
  604.                 else
  605.                     procPrintf( ShellWh, ProcID, "rmr : bad arguments.\n" );
  606.  
  607.                 /* Tell the shell that we're done */
  608.                 SendOutToken(  ShellWh, ProcID, PROC_BREAK );
  609.                 
  610.                 /* Turn ourself off */
  611.                 (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  612.                 return( FALSE );
  613.                 }
  614.         }
  615. }
  616.  
  617. /*******************************************************************
  618.  
  619.     grepr [options] string files
  620.     
  621.     grepr searchs resource files for resources that contain the specified
  622.     string.  Resource TYPE, ID and offset are printed.
  623.     
  624.     Patterns may be used in the files specification.
  625.  
  626.     options
  627.         -t    TYPE    where TYPE is a 4 character file type
  628.         -i  ID        where ID is a decimal resource ID
  629.         
  630.  *******************************************************************/    
  631.                 
  632. #define        greprPatLen        (**MyShell).Proc[ProcID].long0
  633. #define        greprResType    (**MyShell).Proc[ProcID].long1
  634. #define        greprIndex        (**MyShell).Proc[ProcID].long2
  635.  
  636. #define        greprIDStart    (**MyShell).Proc[ProcID].int0
  637. #define        greprIDEnd        (**MyShell).Proc[ProcID].int1
  638. #define        greprCheckT        (**MyShell).Proc[ProcID].int2
  639.  
  640. #define        greprTypes        (**MyShell).Proc[ProcID].bflags.f0
  641. #define        greprIDs        (**MyShell).Proc[ProcID].bflags.f1
  642. #define        greprData        (**MyShell).Proc[ProcID].bflags.f2
  643. #define        abortGrepr        (**MyShell).Proc[ProcID].bflags.f3
  644.                 
  645. extern     char     resName[],
  646.                 resAttrStr[];
  647.                 
  648. extern    char    grepPattern[];
  649.  
  650. /*******************************************************************/
  651.  
  652. Boolean    BinaryMatch( Ptr base, Ptr check, int32 len )
  653. {
  654.     while( len )
  655.         {
  656.         if( *base != *check )
  657.             break;
  658.             
  659.         len--;
  660.         base++;
  661.         check++;        
  662.         }
  663.  
  664.     return( len == 0 );
  665. }
  666.  
  667. Boolean    greprHeadPrinted;
  668.  
  669. void    grepResource( WHandle ShellWh, int16 ProcID, Handle h,
  670.                 OsType theType, int16 resID, char *str )        
  671. {
  672. Ptr        b, p;
  673. int32    totLen;
  674. char     typeStr[ 6 ];
  675. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  676.  
  677.     greprIndex = 0L;
  678.  
  679.     MoveHHi( h );
  680.     HLock( h );
  681.     b = *h;
  682.     
  683.     totLen = SizeResource( h ) - greprPatLen;
  684.     TypeToStr( theType, typeStr );
  685.     
  686.     while( (totLen > 0) && !abortGrepr )
  687.         {
  688.         p = grepPattern;
  689.         
  690.         if( BinaryMatch( b, p, greprPatLen ) )
  691.             {
  692.             if( !greprHeadPrinted )
  693.                 {
  694.                 procPrintf( ShellWh, ProcID,
  695.                     "Offset  TYPE   ID   Name\n--------------------------------\n" );
  696.                 greprHeadPrinted = TRUE;
  697.                 }
  698.  
  699.             procPrintf( ShellWh, ProcID, "%5ld : %4s  %5d %ps\n",
  700.                 greprIndex, typeStr, resID, str );
  701.             }
  702.         
  703.         greprIndex++;
  704.         totLen--;
  705.         b++;
  706.         }
  707.     
  708.     HUnlock( h );
  709. }
  710.  
  711. /*******************************************************************/
  712.  
  713. void        greprType( WHandle ShellWh, int16 ProcID, OsType theType,
  714.                  int16 numRes )
  715. {
  716. OsType        aType;
  717. int16        resID;
  718. int32        resLoaded;
  719. Handle        h;
  720. char        str[ 64 ];
  721. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  722.  
  723.     while( --numRes && !abortGrepr )
  724.         {
  725.         h = Get1IndResource( theType, numRes );
  726.         
  727.         if( h )
  728.             {
  729.             GetResInfo( h, &resID, &aType, str );
  730.             
  731.             if( greprCheckT == 2 )
  732.                 {
  733.                 if( resID < greprIDStart )
  734.                     continue;
  735.                 else if( resID > greprIDEnd )
  736.                         continue;
  737.                 }
  738.             else if( greprCheckT == 1 )
  739.                 if( resID != greprIDStart )
  740.                     continue;
  741.             
  742.             
  743.             resLoaded = (int32) *h;
  744.             LoadResource( h );
  745.             
  746.             grepResource( ShellWh, ProcID, h, aType, resID, str );    
  747.             
  748.             if( !resLoaded )        
  749.                 ReleaseResource( h );
  750.             }
  751.             
  752.         if( UserAbort() )
  753.             abortGrepr = TRUE;
  754.         }
  755. }
  756.  
  757. /*******************************************************************/
  758.  
  759. void        greprName( WHandle ShellWh, int16 ProcID, 
  760.                  OsType theType, char *theName )
  761. {
  762. OsType        aType;
  763. Handle        h;
  764. char        str[ 64 ];
  765. int32        resLoaded;
  766. int16        resID;
  767. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  768.  
  769.     strcpy( str, theName );
  770.     CtoPstr( str );
  771.     h = Get1NamedResource( theType, str );
  772.     
  773.     if( h )
  774.         {
  775.         GetResInfo( h, &resID, &aType, str );
  776.     
  777.         if( greprCheckT == 2 )
  778.             {
  779.             if( resID < greprIDStart )
  780.                 return;
  781.             else if( resID > greprIDEnd )
  782.                     return;
  783.             }
  784.         else if( greprCheckT == 1 )
  785.             if( resID != greprIDStart )
  786.                 return;
  787.             
  788.         resLoaded = (int32) *h;
  789.         LoadResource( h );
  790.         
  791.         grepResource( ShellWh, ProcID, h, aType, resID, str );    
  792.         
  793.         if( !resLoaded )        
  794.             ReleaseResource( h );
  795.         }
  796. }
  797.  
  798. /*******************************************************************/
  799.  
  800. void        GREPRCallBack( WHandle ShellWh, int16 ProcID, char *path,
  801.                 char *last, pathType what, int16 vRefNum, int32 dirID )
  802. {
  803. char        str[ 256 ];
  804. OSErr        err;
  805. int16        refNum, currFile, numTypes, numRes, resID,
  806.             resFileWasOpen = FALSE;
  807. OsType        theType, aType;
  808. Handle        h;
  809. Boolean        fileName = TRUE;
  810. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  811.  
  812.     if( what == pathIsFile )
  813.         {
  814.         strcpy( str, last );
  815.         CtoPstr( str );
  816.         
  817.         err = HSetVol( NULL, vRefNum, dirID );
  818.         
  819.         if( err )
  820.             return;
  821.             
  822.         currFile = CurResFile();
  823.         if( ResFileOpen( last, vRefNum, dirID, &refNum ) )
  824.             resFileWasOpen = TRUE;
  825.         else
  826.             refNum = OpenResFile( str );
  827.         
  828.         if( refNum != (-1) )
  829.             {
  830.             greprHeadPrinted = FALSE;
  831.             UseResFile( refNum );
  832.             SetResLoad( FALSE );
  833.             numTypes = Count1Types() + 1;
  834.             
  835.             while( --numTypes && !abortGrepr )
  836.                 {
  837.                 if( UserAbort() )
  838.                     abortGrepr = TRUE;
  839.  
  840.                 Get1IndType( &theType, numTypes );    /* What type is this ? */
  841.                 TypeToStr( theType, str );
  842.                 
  843.                 numRes = Count1Resources( theType ) + 1;
  844.                 
  845.                 if( greprResType && (greprResType != theType ) )
  846.                     continue;
  847.                 
  848.                 if( fileName )
  849.                     {
  850.                     procPrintf( ShellWh, ProcID, "grepr : %s\n", last );
  851.                     fileName = FALSE;
  852.                     }
  853.                         
  854.                 if( *resName )
  855.                     greprName( ShellWh, ProcID, theType, resName );
  856.                 else
  857.                     greprType( ShellWh, ProcID, theType, numRes );
  858.                 }
  859.                 
  860.             if( !resFileWasOpen )
  861.                 CloseResFile( refNum );
  862.                 
  863.             SetResLoad( TRUE );
  864.             UseResFile( currFile );
  865.             }
  866.         else if( (err = ResError()) && (err != eofErr) )
  867.             procPrintf( ShellWh, ProcID,
  868.                 "grepr : error openning file %s (%d)\n", last, err );
  869.         }
  870. }
  871.  
  872. /*******************************************************************/
  873.  
  874. void        GREPRFile( WHandle ShellWh, int16 ProcID, char *argument )
  875. {
  876. ShellWindRec    **MyShell;
  877.  
  878.     MyShell = (ShellWindRec **) (**ShellWh).thing;
  879.  
  880.     ExpandPath( ShellWh, ProcID, argument, (ProcPtr) GREPRCallBack,
  881.             (**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
  882.  
  883.     ResetShellPWD( ShellWh );
  884. }
  885.  
  886. /*******************************************************************/
  887.  
  888. Boolean            DoGREPR( int16 ProcToken, WHandle ShellWh, int16 ProcID,
  889.                     char *string )
  890. {
  891. int16            i, argc, gotPattern = FALSE, badIDs = FALSE, patIsHEX = FALSE;
  892. char            *cp, argument[ 256 ];
  893. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  894.  
  895.     switch( ProcToken )
  896.         {
  897.         case    PROC_INIT    :
  898.             (**MyShell).Proc[ ProcID ].flags = TRUE;
  899.             break;
  900.             
  901.         case    PROC_TERM    :
  902.         case    PROC_BREAK    :
  903.             /* Tell the shell that we're done */
  904.             SendOutToken( ShellWh, ProcID, PROC_BREAK );
  905.             /* Turn ourself off */
  906.             (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  907.             break;
  908.             
  909.         case    PROC_STDIN    :
  910.             if( (**MyShell).Proc[ ProcID ].flags )
  911.                 {
  912.                 (**MyShell).Proc[ ProcID ].flags = FALSE;        
  913.  
  914.                 /* get arguments */
  915.                 argc = (**MyShell).Proc[ ProcID ].argc;
  916.                 greprTypes        = TRUE;
  917.                 greprIDs        = TRUE;
  918.                 greprData        = TRUE;
  919.                 greprCheckT        = 0;
  920.                 greprIDStart    = 0;
  921.                 greprIDEnd        = 0;
  922.                 greprResType    = 0L;        /* no type specifier */
  923.                 *resName        = '\0';
  924.                 abortGrepr        = FALSE;
  925.                 
  926.                 for( i = 1; i < argc; i++ )
  927.                     {
  928.                     GetArgv( ShellWh, ProcID, i, argument );
  929.                     cp = argument;
  930.         
  931.                     if( *cp++ == '-' )
  932.                         while( *cp )
  933.                             switch( *cp++ )
  934.                                 {
  935.                                 case    'h'    :    /* Hex */
  936.                                     patIsHEX = TRUE;
  937.                                     break;
  938.                                     
  939.                                 case    'n'    :    /* Name */
  940.                                     i++;
  941.                                     GetArgv( ShellWh, ProcID, i, resName );
  942.                                     break;
  943.  
  944.                                 case    'i'    :    /* id's */
  945.                                     i++;
  946.                                     if( i < argc )
  947.                                         {
  948.                                         char    *cp, nc, temp[ 64 ], num[ 64 ];
  949.                                         int32    lnum;
  950.                                         
  951.                                         GetArgv( ShellWh, ProcID, i, temp );
  952.                                         
  953.                                         cp = temp;
  954.                                         while( *cp && (*cp != ',') )
  955.                                             cp++;
  956.                                         
  957.                                         nc = *cp;
  958.                                         
  959.                                         if( *cp )
  960.                                             *cp = '\0';
  961.                                         
  962.                                         CtoPstr( temp );
  963.                                         if( GoodNum( temp ) )
  964.                                             {
  965.                                             StringToNum( temp, &lnum );
  966.                                             greprIDStart = (int16) lnum;
  967.                                             greprCheckT++;
  968.  
  969.                                             if( nc )
  970.                                                 {
  971.                                                 cp++;
  972.                                                 CtoPstr( cp );
  973.                                                 if( GoodNum( cp ) )
  974.                                                     {
  975.                                                     StringToNum( cp, &lnum );
  976.                                                     greprIDEnd = (int16) lnum;
  977.                                                     greprCheckT++;
  978.                                                     }
  979.                                                 else
  980.                                                     badIDs = TRUE;
  981.                                                 }
  982.                                             }
  983.                                         else
  984.                                             badIDs = TRUE;
  985.                                         }
  986.                                     break;
  987.  
  988.                                 case    't'    :        /* Type */
  989.                                     i++;
  990.                                     if( i < argc )
  991.                                         {
  992.                                         char    temp[ 256 ];
  993.                                         
  994.                                         GetArgv( ShellWh, ProcID, i, temp );
  995.                                         greprResType = StrToType( temp );
  996.                                         }
  997.                                     break;
  998.                                 }
  999.                     }
  1000.  
  1001.                 if( badIDs )
  1002.                     procPrintf( ShellWh, ProcID, "grepr : bad resource ID\n" );
  1003.                 else
  1004.                     for( i = 1; i < argc; i++ )
  1005.                         {
  1006.                         GetArgv( ShellWh, ProcID, i, argument );
  1007.                         if( *argument != '-' )
  1008.                             {
  1009.                             if( gotPattern )
  1010.                                 GREPRFile( ShellWh, ProcID, argument );
  1011.                             else
  1012.                                 {
  1013.                                 if( patIsHEX )
  1014.                                     {
  1015.                                     greprPatLen = strlen( argument ) / 2;
  1016.                                     CtoPstr( argument );
  1017.                                     StuffHex( grepPattern, argument );
  1018.                                     }
  1019.                                 else
  1020.                                     {
  1021.                                     strcpy( grepPattern, argument );
  1022.                                     greprPatLen = strlen( grepPattern );
  1023.                                     }
  1024.                                 gotPattern = TRUE;
  1025.                                 }
  1026.                             }
  1027.                         else
  1028.                             switch( argument[ 1 ] )
  1029.                                 {
  1030.                                 case    'n'    :
  1031.                                 case    'i'    :
  1032.                                 case    't'    :
  1033.                                     i++;
  1034.                                     break;
  1035.                                 }
  1036.                         }
  1037.  
  1038.                 /* Tell the shell that we're done */
  1039.                 SendOutToken(  ShellWh, ProcID, PROC_BREAK );
  1040.                 
  1041.                 /* Turn ourself off */
  1042.                 (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  1043.                 return( FALSE );
  1044.                 }
  1045.         }
  1046. }